package com.framework.web.bus.controller;


import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import com.framework.web.bus.domain.Ingredients;
import com.framework.web.bus.service.IngredientsService;
import com.framework.web.common.domain.BusinessConstants;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
* @Title: Controller
* @Description: 控制层
* @author fusf
* @date 2016-09-08
* @version V1.0
*
*/
@Controller
@RequestMapping("/ingredients")
public class IngredientsController {

private Logger logger = LoggerFactory.getLogger(this.getClass());


/**
* Spring注入资源信息模块service
*/
@Autowired
private IngredientsService ingredientsService;



/**
*
* 分页
* @param dgm
* @param ingredients
* @return
* @throws Exception
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> list(DataGridModel dgm, Ingredients ingredients, HttpServletRequest request) throws Exception {
    PageRequest pr = DataGridModel.getPageRequest(dgm);
    return PageUtil.getPageMap(ingredientsService.getPageList(ingredients, pr));
}


/**
*  请求新增或修改页面
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/toSaveAndUpdate/{cookbookId}/{id}", method = RequestMethod.GET)
public ModelAndView toSaveAndUpdate(@PathVariable("cookbookId") String cookbookId,@PathVariable("id") String id)
throws Exception {
    ModelAndView modelAndView = new ModelAndView("/bus/ingredients/ingredientsForm");
    Ingredients ingredients = new Ingredients();
    if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
        ingredients = ingredientsService.findById(id);
    } else {

    }
    ingredients.setCookbookId(cookbookId);
    modelAndView.addObject("ingredients", ingredients);
    return modelAndView;
}


/**
* 保存信息
*
* @param ingredients  信息实体
* @return
* @throws Exception
*/
@RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
@ResponseBody
public AjaxJson saveAndUpdate(HttpServletRequest request,HttpServletResponse response,
                              Ingredients ingredients) throws Exception {
    AjaxJson j = new AjaxJson();
    if (StringUtils.isNotEmpty(ingredients.getId())) {//更新
        Ingredients r = ingredientsService.findById(ingredients.getId());
        MyBeanUtils.copyBeanNotNull2Bean(ingredients, r);
        ingredients = r;
    }else {

    }
    /*初始化参数*/
    ingredients.setStatus(BusinessConstants.STATUS.NORMAL);
    ingredientsService.save(ingredients);
    j.setMsg("保存成功");
    return j;
}

/**
* 根据id字符串删除数据信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value ="/delete", method = RequestMethod.POST)
@ResponseBody
public AjaxJson delete(String id) throws Exception {
    AjaxJson j = new AjaxJson();
    ingredientsService.delete(id);
    j.setMsg("删除成功");
    return j;
}

@RequestMapping("/toDatagrid/{id}")
@ResponseBody
public ModelAndView toDatagrid(HttpServletRequest request,HttpServletResponse response,@PathVariable("id")String cookbookId){
    ModelAndView mav = new ModelAndView("/bus/ingredients/ingredientsList");
    mav.addObject("cookbookId",cookbookId);
    return mav;
}

}